﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; 


namespace SharpSphinx.Util
{


    /// <summary>
    /// 
    /// </summary>
	public class BatchFile
	{


        /// <summary>
        /// Returns a List of the lines in a batch file.
        /// </summary>
        /// <param name="batchFile">the batch file to read</param>
        /// <returns>a List of the lines in a batch file</returns>

        public static List<String> getLines(String batchFile)  {
             
            try
            {
               return  getLines(batchFile, 0);

            }
            catch (IOException e)
            {
                throw new IOException(""); 

            }


       }

        /// <summary>
        /// a List of the lines in a batch file
        /// </summary>
        /// <param name="batchFile">the batch file to read</param>
        /// <param name="skip">the number of lines to skip between items</param>
        /// <returns>a List of the lines in a batch file</returns>
        public static List<String> getLines(String batchFile, int skip)
        {
            int curCount = skip;
            List<String> list = new List<string>();
            StreamReader reader = new StreamReader(batchFile);
            string line = null;

            while ((line = reader.ReadLine()) != null)
            {
                if (line.Count() != 0)
                {
                    if (++curCount >= skip)
                    {
                        list.Add(line);
                        curCount = 0;
                    }

                }

            }
            reader.Close();
            return list;

        }

        /// <summary>
        /// the file name portion of a line in a batch file. This is the portion of the line before the first space.
        /// </summary>
        /// <param name="batchFileLine"></param>
        /// <returns>the file name portion of a line in a batch file.</returns>
        public static string getFilename(string batchFileLine)
        {
            int firstSpace = batchFileLine.IndexOf(' ');
            return batchFileLine.Substring(0, firstSpace).Trim(); 
        }


        /// <summary>
        /// Returns the reference string portion of a line in a batch file. This is the portion of the line after the first space
        /// </summary>
        /// <param name="batchFileLine"></param>
        /// <returns>the reference string portion of a line in a batch file</returns>
        public static String getReference(string batchFileLine)
        {
            int firstSpace = batchFileLine.IndexOf(' ');
            return batchFileLine.Substring(firstSpace + 1).Trim(); 

        }

	}
}
